package org.flintparticles.twoD.renderers
{
	
	
	import flash.display.Stage;
	import flash.events.Event;
	
	import org.flintparticles.common.particles.Particle;
	import org.flintparticles.common.renderers.RendererBase;
	import org.flintparticles.common.stage3d.Stage3DManager;
	import org.flintparticles.common.stage3d.utils.Color;
	import org.flintparticles.common.stage3d.utils.Stage3DMath;
	import org.flintparticles.twoD.particles.Particle2D;
	import org.flintparticles.twoD.stage3d.images.GeomEntity2D;
	import org.flintparticles.twoD.stage3d.images.Image2D;
	import org.flintparticles.twoD.stage3d.images.Polygon2D;
	
	public class Stage3DRenderer extends RendererBase
	{
		
		//private var _context:Context3D;
		private var _sceneGraph:Vector.<GeomEntity2D>=new Vector.<GeomEntity2D>();
		private var _stageMath:Stage3DMath;
		private var _stage3Manager:Stage3DManager;
		private var _canRender:Boolean=false;
		private var _stage:Stage;
		private var _bgColor:uint=0xffffff;
		public function Stage3DRenderer(stage:Stage,bgcolor:uint=0xffffff)
		{
			//_context=context;
			_stage=stage;
			if(bgcolor)_bgColor=bgcolor;
			
			_stageMath=new Stage3DMath();
			_stageMath.setOrthographicProjection(_stage.stageWidth,_stage.stageHeight);
			initStage3DManager();
		}
		private function initStage3DManager():void{
			_stage3Manager=Stage3DManager.getInstance(_stage);
			_stage3Manager.addEventListener("CONTEXT3D_READY",onContextReady);
			_stage3Manager.setStage3dByIndex(0);
		}
		private function onContextReady(e:Event):void{
			
			_canRender=true;
		}
		
		override protected function renderParticles(particles:Array):void{
			if(!_canRender)return;
			
			for each( var p:Particle2D in particles){
				
				renderParticle(p);
			}
			clear(_bgColor,1);
			
			
			var length:uint=_sceneGraph.length;
		    if(length<=0)return;
			for(var i:uint=0;i<length;++i){
				_sceneGraph[i].draw(_stageMath);
			}
			_stage3Manager.context3D.present();
			_stageMath.resetMatrix();
			
			
		}
		protected function renderParticle(particle:Particle2D):void{
			var o:*=particle.image;
			if(o is Polygon2D){
				Polygon2D(o).x=particle.x;
				Polygon2D(o).y=particle.y;
				//	trace(particle.x+"//"+particle.y);
			
				Polygon2D(o).rotation=particle.rotation;
				Polygon2D(o).scaleX=Polygon2D(o).scaleY=particle.scale;
			//	trace(particle.rotation);
				Polygon2D(o).color=particle.color&0xffffff;
				Polygon2D(o).alpha=particle.alpha;
				
				
			}
			if(o is Image2D){
				Polygon2D(o).x=particle.x;
				Polygon2D(o).y=particle.y;
				Polygon2D(o).rotation=particle.rotation;
				Polygon2D(o).scaleX=Polygon2D(o).scaleY=particle.scale;
				Polygon2D(o).alpha=particle.alpha;
			}
		}
		override protected function addParticle( particle:Particle ):void
		{
			//renderParticle(particle as Particle2D);
			_sceneGraph.splice(_sceneGraph.length,0,particle.image);
		}
		override protected function removeParticle(particle:Particle):void{
			if(_sceneGraph&&_sceneGraph.length>0){
				var childIndex:int=_sceneGraph.indexOf(particle.image);
				_sceneGraph.splice(childIndex,1);
				//	Polygon2D(particle.image).dispose();
			}
			//Polygon2D(particle.image).dispose();
		}
		public function clear(rgb:uint=0, alpha:Number=1.0):void
		{
			_stage3Manager.context3D.clear(
				Color.getRed(rgb)   / 255.0, 
				Color.getGreen(rgb) / 255.0, 
				Color.getBlue(rgb)  / 255.0,
				alpha);
			
		}
	}
}